package view;

/**
 * @author Andrea
 * 
 */
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class Pen {
	ResourceBundle myViewLabels = ResourceBundle
			.getBundle("resources.englishView");
	ResourceBundle myIcons = ResourceBundle
			.getBundle("resources.slogoMultimedia");
	private JRadioButton radioButton;
	private String selectedLine;
	private String selectedThickness;

	public void execute() {
		JFrame popupFrame = new JFrame(myViewLabels.getString("PenPopupFrame"));
		popupFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel radioClusters = new JPanel(new BorderLayout());
		radioClusters.add(makeLineOptions(), BorderLayout.WEST);
		radioClusters.add(makeThicknessOptions(), BorderLayout.EAST);
		popupFrame.add(radioClusters, BorderLayout.NORTH);

		popupFrame.add(makeSaveButton("Save Changes"), BorderLayout.SOUTH);

		popupFrame.pack();
		popupFrame.setVisible(true);
	}

	private JPanel makeLineOptions() {
		JPanel linePanel = new JPanel(new BorderLayout());
		ButtonGroup lineGroup = new ButtonGroup();
		JRadioButton lineButton = makeLineButton(
				myIcons.getString("SolidLine"), myViewLabels
						.getString("SolidLine"), true);
		linePanel.add(lineButton, BorderLayout.NORTH);
		lineGroup.add(lineButton);

		lineButton = makeLineButton(myIcons.getString("DashedLine"),
				myViewLabels.getString("DashedLine"), false);
		linePanel.add(lineButton, BorderLayout.SOUTH);
		lineGroup.add(lineButton);

		linePanel.setBorder(BorderFactory.createTitledBorder(myViewLabels
				.getString("LinesBorder")));

		return linePanel;
	}

	private JPanel makeThicknessOptions() {
		JPanel thicknessPanel = new JPanel(new BorderLayout());
		ButtonGroup thicknessGroup = new ButtonGroup();
		JRadioButton thicknessButton = makeThicknessButton(myIcons
				.getString("ThinLine"), myViewLabels.getString("ThinLine"),
				true);
		thicknessPanel.add(thicknessButton, BorderLayout.NORTH);
		thicknessGroup.add(thicknessButton);

		thicknessButton = makeThicknessButton(myIcons.getString("MediumLine"),
				myViewLabels.getString("MediumLine"), false);
		thicknessPanel.add(thicknessButton, BorderLayout.CENTER);
		thicknessGroup.add(thicknessButton);

		thicknessButton = makeThicknessButton(myIcons.getString("ThickLine"),
				myViewLabels.getString("ThickLine"), false);
		thicknessPanel.add(thicknessButton, BorderLayout.SOUTH);
		thicknessGroup.add(thicknessButton);

		thicknessPanel.setBorder(BorderFactory.createTitledBorder(myViewLabels
				.getString("ThicknessBorder")));

		return thicknessPanel;
	}

	private void makeColorChooser() {

	}

	private JRadioButton makeLineButton(String icon, String actionCommand,
			boolean selected) {
		ImageIcon buttonIcon = new ImageIcon(icon);
		radioButton = new JRadioButton(buttonIcon);
		radioButton.setActionCommand(actionCommand);
		radioButton.setSelected(selected);
		radioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectedLine = e.getActionCommand();
				System.out.println("CLICKED LINES!");
				System.out.println(selectedLine);
			}
		});
		return radioButton;
	}

	private JRadioButton makeThicknessButton(String icon, String actionCommand,
			boolean selected) {
		ImageIcon buttonIcon = new ImageIcon(icon);
		radioButton = new JRadioButton(buttonIcon);
		radioButton.setActionCommand(actionCommand);
		radioButton.setSelected(selected);
		radioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectedThickness = e.getActionCommand();
				System.out.println("CLICKED THICKNESSES!");
				System.out.println(selectedThickness);
			}
		});
		return radioButton;
	}

	private JButton makeSaveButton(String name) {
		JButton saveButton = new JButton(name);

		saveButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// Send the changes in pen over to the Model

			}
		});

		return saveButton;
	}

}
